草庐IT

c++ - 指针与引用

全部标签

go - grpc:使用 oneof 会导致无效的内存地址或 nil 指针取消引用

我正在尝试使用Go将proto3结构发送到gRPC服务器。该结构有一个oneof类型,我似乎很好地填充了它。将消息发送到我的gRPC客户端时,我对无效内存地址或nil指针引用感到panic。我有原型(prototype)定义(完整文件位于https://github.com/MovingGauteng/geofancy-rs/blob/master/proto/geofancy.proto:#proto3messageDocument{stringcollection=1;stringid=2;oneofgeo{Pointpoint=4;LineStringline=5;Boundsb

go - 使用反射填充指向结构的指针

我想遍历结构中的字段并提示将字符串值输入字符串字段,对作为结构指针的字段递归执行此操作。目前这是我尝试过的方法,但在尝试在指针的字符串字段中设置此值时出现错误。packagemainimport("fmt""reflect")typeTablestruct{PK*Field}typeFieldstruct{Namestring}funcmain(){PopulateStruct(&Table{})}funcPopulateStruct(ainterface{})interface{}{typeOf:=reflect.TypeOf(a)valueOf:=reflect.ValueOf(a

go - 是否可以捕获此 nil 指针错误

我有一个编译器无法发现的“nilpointer”错误,但我想看看是否有办法通过静态分析找到它。所以bug是这样的:packagemainimport("fmt")typeAstruct{namestring}funcnewGoodA()(*A,error){return&A{name:"Go",},nil}funcnewBadA()(*A,error){returnnil,fmt.Errorf("failedtocreateA")}func(a*A)greet()string{return"Hello"+a.name}funcmain(){valueA,err:=newBadA()if

pointers - 类型将接口(interface)指针转换为接口(interface)指针

这个问题在这里已经有了答案:Whycan'tIgettheaddressofatypeconversioninGo?(1个回答)2年前关闭。我正在尝试将接口(interface)指针(*B)转换为其他接口(interface)指针(*A)。A是B的“父”所以B具有A的所有功能有。我在谷歌搜索并找到了“类型断言”,但在这种情况下我不能这样做。我试过了:f(b.(A))f(b.(*A))f((*b).(A))f(&(*b).(A))但唯一有效的是:tmp:=(*b).(A)f(&tmp)然而它复制b它没有优化。typeAinterface{foo()}typeBinterface{Abar

go - 对结构中字段的外部引用是否会阻止该结构被垃圾收集?

例如,如果我有一些typeAstruct{Bstring;Cint},我有一个funcfoo(aA)*string{return&a.B},我用c:=foo(a),a是否必须保留在上下文中,直到c可收集?或者,如果我定义funcfoo1(aA)*string{s:=a.B;return&s这对何时可以收集A有什么影响吗? 最佳答案 在第一个例子中,假设a在调用foo(a)之后没有被使用,a是可收集的,因为你通过了a按值。该函数返回一个指向a副本中的值的指针,因此a变得可收集,但在函数foo中创建的副本不可收集收藏品。现在,如果您将&

map - 在 Go 中清除具有指针值的映射

我有一个map[string]*list.List并且每个列表节点也是一个指针。通过简单地将map清除为nil,所有map和列表以及所有这些指针是否都会被清除并收集垃圾并准备好再次使用?typeUnrolledGroupstruct{nextints[]uint32}vardictionary=struct{mmap[string]*list.Listkeys[]string}{m:make(map[string]*list.List)}l:=list.New()newGroup:=UnrolledGroup{next:1,s:make([]uint32,groupLen)}newGr

reference - 另一种类型中自定义类型的 Golang slice 作为引用

我的Go测试代码出现此错误:$goruntest.go#command-line-arguments./test.go:43:cannotuse&ol1(type*Orderline)astypeOrderlineinarrayelement./test.go:43:cannotuse&ol2(type*Orderline)astypeOrderlineinarrayelement代码packagemainimport("fmt")typeCustomerstruct{Idint64Namestring}typeOrderstruct{Idint64Customer*CustomerO

go - 设置指针的值不能通过接口(interface){}

下面是来自go反射法则的稍微修改的示例http://blog.golang.org/laws-of-reflection.第二个代码部分使用了来自map[string]interface{}的指针,但它不起作用,我做错了什么?谢谢//http://play.golang.org/p/LuMBUWLVT6packagemainimport("fmt""reflect")typeTstruct{xfloat64}func(xT)RowMap()map[string]interface{}{returnmap[string]interface{}{"x":&x.x,}}funcmain(){

c - 如何将 Go 绑定(bind)建模为使用 union 的 C 结构?

我目前正在写一个Gowrapper对于libfreefare.libfreefare的API包含以下功能:structmifare_desfire_file_settings{uint8_tfile_type;uint8_tcommunication_settings;uint16_taccess_rights;union{struct{uint32_tfile_size;}standard_file;struct{int32_tlower_limit;int32_tupper_limit;int32_tlimited_credit_value;uint8_tlimited_credi

go - 通过来自 channel 值的指针分配给局部变量

代码在这里:http://play.golang.org/p/WjpgN_0AaP在第45、46和47行,有三种不同的方法可以从消息代理中提取值(value)。//varmbMessageBroker=*所有这三个都具有完全相同的结果。选择一种方式而不是另一种方式有什么意义?另外,我很困惑为什么星号看起来没有什么区别。 最佳答案 查看函数声明:func(c*MyGui)Receive(in我们可以看到会给你一个*MessageBroker类型的值,指向MessageBroker的指针结构。在指针值之前放置一个星号将取消引用它(参见G